﻿#include "pch.h"

CTickmarkManager::CTickmarkManager()
{
    memset((void *)this, 0, sizeof(CTickmarkManager));
}

void CTickmarkManager::printSelf(char *captionUpper, int32_t)
{
    char caption[1024];
    sprintf(caption, "%s:%s", captionUpper, "CTickmarkManager");
    print4x(this, sizeof(CTickmarkManager), caption);
    if (this->head)
    {
        sprintf(caption, "%s:%s", captionUpper, "CTickmarkManager.head");
        print4x(this->head, this->size * this->len, caption);
    }
    if (this->num)
    {
        sprintf(caption, "%s:%s", captionUpper, "CTickmarkManager.num");
        print4x(this->num, sizeof(int32_t), caption);
    }
}

// sub_1001B1E0
CTickmarkManager *CTickmarkManager::init(int32_t len)
{
    init_(len);
    return this;
};

// sub_1001DF60
CTickmarkManager *CTickmarkManager::init_(int32_t len)
{
    this->needFreeHead = true;
    this->head = (LPVOID) new TickInf[len];
    if (this->head)
    {
        this->size = sizeof(TickInf);
        this->len = len;
        memset((void *)this->head, 0, sizeof(TickInf) * len);
        this->num = &(this->numValue);
        *(this->num) = 0;
        this->tail = 0;
    }
    return this;
}
// sub_10001000
int32_t CTickmarkManager::setFirstTickmark(int32_t time, TICKMARK tickmark)
{
    if (!this->incTickmarkNum())
    {
        return false;
    }
    this->getTickInf(0)->time = time;
    this->getTickInf(0)->tickmark = tickmark;
    this->addTail(1);
    return true;
}
// sub_10024F90
int32_t CTickmarkManager::incTickmarkNum()
{
    if (*(this->num) >= this->len)
    {
        return false;
    }
    memset((void *)(this->size * *(this->num) + (BYTE *)this->head), 0, this->size);
    ++*(this->num);
    return true;
}

// sub_1001E060
TickInf *CTickmarkManager::getTickInf(int32_t idx)
{
    int32_t n;
    TickInf *pMark = (TickInf *)this->head;

    if (idx)
    {
        n = idx;
    }
    else
    {
        n = this->tail;
    }
    return &pMark[n];
}

// sub_1001E040
int32_t CTickmarkManager::addTail(int32_t idx)
{
    return this->setTail(idx + this->tail);
}

// sub_1000104B
CTickmarkManager *CTickmarkManager::callbackDrawTickMark(int32_t chn)
{
    int32_t i;
    TickInf *pMark = (TickInf *)this->head;

    if (fnDrawTickMark)
    {
        for (i = 0; i < this->numValue; ++i)
        {
            fnDrawTickMark(chn, pMark[i].time, pMark[i].tickmark);
        }
        this->tail = 0;
        this->numValue = 0;
    }
    return this;
}
